//
//  FindPathThirdViewController.m
//  
//
//  Created by 郭明皓 on 15/10/30.
//
//

#import "FindPathThirdViewController.h"
#define WIDTH self.view.frame.size.width
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "GMHPath.h"
#import "GMHDataFiled.h"
#import "Sqlite.h"
#import "GMH_Share.h"
#import "DKNightVersion.h"
@interface FindPathThirdViewController ()
@property (nonatomic, retain)NSMutableArray *PicturesArr;
@property (nonatomic, retain)UIScrollView *scroll;
@property (nonatomic, retain)UIScrollView *backScroll;
@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic, retain)UILabel *piclabel;
@property (nonatomic, assign)NSInteger flag;
@end

@implementation FindPathThirdViewController
- (void)dealloc
{
    [_piclabel release];
    [_backScroll release];
    [_dic release];
    [_PicturesArr release];
    [_scroll release];
    [super dealloc];
}

- (void)create
{
    [GMHDataFiled isHaveFileDocument];
    [GMHDataFiled isHaveFileFind];
    
    NSString *str = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v2/trail/%ld?&device_type=2&trail_id=%ld&app_version=4.3.4&api_version=1", (long)_number, (long)_number];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.dic = responseObject;
        self.PicturesArr = [GMHPath baseModelArry:self.dic[@"pictures"] ];
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
        CGRect rect = [self.dic[@"detail"] boundingRectWithSize:CGSizeMake(WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        self.backScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, self.view.frame.size.height - 64 - 44)];
        [self.view addSubview:self.backScroll];
        [_backScroll release];
        self.backScroll.contentSize = CGSizeMake(0, 314 + rect.size.height);

        //轮播图
        self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 175)];
        [self.backScroll addSubview:self.scroll];
        [self.scroll release];
        self.scroll.contentSize = CGSizeMake(self.PicturesArr.count * WIDTH, 0);
        self.scroll.pagingEnabled = YES;
        
        for (NSInteger i = 0; i < self.PicturesArr.count; i++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, 200)];
            NSString *pictures = [NSString stringWithFormat:@"%@", [self.PicturesArr[i] picture]];
            [imageView sd_setImageWithURL:[NSURL URLWithString:pictures]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
            [self.scroll addSubview:imageView];
            
            self.piclabel = [[UILabel alloc]initWithFrame:CGRectMake(i * WIDTH + 20, 150, 70, 20)];
            [self.scroll addSubview:self.piclabel];
            [self.piclabel release];
            self.piclabel.text = [NSString stringWithFormat:@"%ld/%lu", (long)i,(unsigned long)self.PicturesArr.count];
            self.piclabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
            self.piclabel.textColor = [UIColor whiteColor];
        }
        
        
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 180, WIDTH - 20, 30)];
        [self.backScroll addSubview:label];
        [label release];
        label.text = self.dic[@"name"];
        label.font = [UIFont systemFontOfSize:20];
        
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 210, WIDTH - 20, 20)];
        [self.backScroll addSubview:label1];
        [label1 release];
        label1.text = self.dic[@"destination"];
        label1.font = [UIFont systemFontOfSize:12];
        
        
        
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 250, WIDTH - 20, rect.size.height )];
        [self.backScroll addSubview:label2];
        [label2 release];
        label2.text = self.dic[@"detail"];
        label2.numberOfLines = 0;
        [label2 sizeToFit];

        if ([GMH_Share SingleShare].isNight == YES) {
            self.scroll.backgroundColor = [UIColor blackColor];
            label.textColor = [UIColor darkGrayColor];
            label1.textColor = [UIColor darkGrayColor];
            label2.textColor = [UIColor darkGrayColor];
        }
       
        NSString *sendBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSString *CachesPath = [sendBoxPath stringByAppendingPathComponent:@"Travel/Find/PathTitleDetail.plist"];
        [NSKeyedArchiver archiveRootObject:dic toFile:CachesPath];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    if ([GMH_Share SingleShare].isNight == YES) {
        self.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    }
    else
    {
        self.view.backgroundColor = [UIColor whiteColor];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    self.flag = 0;
    UIImageView *backImage = [[UIImageView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:backImage];
    [backImage release];
    backImage.image = [UIImage imageNamed:@"travel.jpg"];
    //毛玻璃效果
    UIBlurEffect *logineffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *logineffectView = [[UIVisualEffectView alloc]initWithEffect:logineffect];
    logineffectView.frame = CGRectMake(0, 0, backImage.frame.size.width, backImage.frame.size.height);
    [backImage addSubview:logineffectView];
    logineffectView.alpha = 1;
    [self create];
    
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timers) userInfo:nil repeats:YES];
    
    
    }
- (void)timers
{
    [self.scroll setContentOffset:CGPointMake(self.scroll.contentOffset.x + WIDTH, 0)];
    if (self.scroll.contentOffset.x == self.PicturesArr.count * WIDTH) {
        self.scroll.contentOffset = CGPointMake(0, 0);
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
